//
//  MyViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/8.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//
#import "RDVTabBarController.h"
#import "MyViewController.h"
#import "TheLoginViewController.h"
#import "ToBindCompanyViewController.h"
#import "HttpRequest.h"
#import "RDVTabBarController.h"
#import "UIImageView+WebCache.h"
#import "AboutUsViewController.h"
#import "UIButton+WebCache.h"
//#import "SalesAnalysisDetailPageViewController.h"
#import "AccountManagementViewController.h"
#import "DiscoverHouseholdMyCollectViewController.h"
#import "MyHeaderView.h"
#import "MemberListModel.h"
#import "MyresultsViewController.h"
@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate, UITextViewDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate,MyHeaderViewDelegate,TheLoginViewControllerDelegate,AccountManagementViewControllerDelegate>

{
    MyHeaderView*HeaderView;
    NSArray *array;
    NSArray *array1;
    NSArray *array2;
    NSArray *array3;
    NSDictionary*dic;
    UITableView*table;
    NSDictionary *DataLine;
}

@end

@implementation MyViewController
@synthesize alt = alt;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"我的";
        
    }
    return self;
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:NO];
    self.navigationController.navigationBarHidden=YES;
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    self.navigationController.delegate = self;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 0)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *arr1 = @[@"我的业绩",@"wodeyeji"];
    NSArray *arr2 = @[@"我的收藏",@"wodeshoucang"];
    NSArray *arr3 = @[@"功能介绍",@"gongnengjieshao"];
    NSArray *arr4 = @[@"邀请好友",@"yaoqinghaoyou"];
    array = @[arr1,arr2];
    array1 = @[arr3,arr4];
    [self requestData];
    [self tableHeadView];

}
-(void)update:(NSString *)updateData{
 [self requestData];
}
-(void)requestData{

    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    self.title =@"";
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
   [HttpRequest requestData1:@"Common/GetUserAuthStatus" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
          if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
              NSLog(@"--%@",severDataDict.dataDict);
                [Global hideProgressHUDInternal:self.view];
               
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                dic=severDataDict.dataDict;
               [user setObject:[severDataDict.dataDict objectForKey:@"headImageUrl"] forKey:@"headImageUrl"];
                [user setObject:[severDataDict.dataDict objectForKey:@"headImage"] forKey:@"headImage"];
                [user setObject:[severDataDict.dataDict objectForKey:@"sex"] forKey:@"sex"];
                [user setObject:[severDataDict.dataDict objectForKey:@"userName"]forKey:@"userName"];
                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"companyName"]]forKey:@"companyName"];
                [user setObject:[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"serverAddress"]] forKey:@"serverAddress"];
                [user setObject:[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"userStatus"]] forKey:@"userStatus"];
                [user setObject:[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"companyId"]] forKey:@"companyId"];
                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"userTypeName"]]  forKey:@"userTypeName"];
                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"userCode"]]  forKey:@"userCode"];
                [user synchronize];
                MemberListModel *dataModel=[MemberListModel dataModelWithUserDict:dic];
                HeaderView.model=dataModel;
                [table reloadData];
                
                if (Token!=nil) {
                    }else{
                  
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        TheLoginViewController*detailViewController = [[TheLoginViewController alloc] init];
                        detailViewController.mDelegate=self;
                        [self.navigationController pushViewController:detailViewController animated:YES];
                    }}}
            else{
                [Global hideProgressHUDInternal:self.view];
                }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
   }];}
- (void)tableHeadView
{
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    HeaderView = [[MyHeaderView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,322/2)];
    HeaderView.mDelegate=self;
    table.tableHeaderView = HeaderView;
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token==nil) {
        
    }else{
  
        UIView * tableFooterViews=[[UIView alloc]initWithFrame:CGRectMake(0, -25, ScreenWidth, 200)];
        tableFooterViews.backgroundColor=zuiqianhuiColor;
        UIView*huiseview=[[UIView alloc]initWithFrame:CGRectMake(0,20, ScreenWidth, 50)];
        huiseview.backgroundColor=[UIColor whiteColor];
        huiseview.layer.borderWidth = 0.3;
        huiseview.layer.borderColor = qianhuiColor.CGColor;
        [tableFooterViews addSubview:huiseview];
        UIButton*exit=[[UIButton alloc]init];
        exit.backgroundColor=[UIColor clearColor];
        [exit setTitle:@"退出登陆" forState:UIControlStateNormal];
        exit. titleLabel.font=[UIFont systemFontOfSize:16];
        [exit setTitleColor:shenhongseColor forState:UIControlStateNormal];
        exit.frame=CGRectMake(0, 0, ScreenWidth, 50);
        [exit addTarget:self action:@selector(exit:) forControlEvents:UIControlEventTouchUpInside];
        [huiseview addSubview:exit];
        table.tableFooterView = tableFooterViews;
        
    }
    
    table.backgroundColor=zuiqianhuiColor;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
}



-(void)exit:(UIButton*)sender{
    
    UIAlertView* alts = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"是否确认注销？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [alts show];
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        
        NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
        NSString* tokenId = [user1 objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:tokenId forKey:@"tokenId"];
        [HttpRequest requestData1:@"Common/LoginOut" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    
                    [Global hideProgressHUDInternal:self.view];
            
                    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                    [user setObject:@"out" forKey:@"tokenId"];
                    
                    [user synchronize];
                    
                    TheLoginViewController* Login=[[TheLoginViewController alloc]init];
                    Login.mDelegate=self;
                    [self.navigationController pushViewController:Login animated:YES];
                
                }else{
            
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHudSub:requestResultStr view:self.view];}
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
            
        }];
    }}
-(void)certificationBtn:(NSString*)certificationBtn{
    
    ToBindCompanyViewController * Login=[[ToBindCompanyViewController alloc]init];
    [self.navigationController pushViewController:Login animated:YES];
    }
-(void)store:(NSString*)store{
    
    AccountManagementViewController * Login=[[AccountManagementViewController alloc]init];
    Login.mDelegate=self;
    [self.navigationController pushViewController:Login animated:YES];
    }

#pragma mark--TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2 ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return array.count;
    }if (section == 1) {
        return array1.count;
    }
    return array1.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    
    cell.textLabel.font=[UIFont systemFontOfSize:15];
    cell.textLabel.textColor=[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1]
    ;
    if (indexPath.section == 0) {
        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(ScreenWidth-30,17.5, 8, 13);
        [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
        [cell.contentView addSubview:jiantou];
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[[array objectAtIndex:indexPath.row] objectAtIndex:1]]];
        cell.textLabel.text = [[array objectAtIndex:indexPath.row] objectAtIndex:0];
        
        if (indexPath.row==0) {
            UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.35)];
            lineLabel.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel];
            
        }
        
        if ([[array objectAtIndex:indexPath.row] count ] !=indexPath.row+1) {
            UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(50, 50-0.35, ScreenWidth-65, 0.3)];
            lineLabel.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel];
            
        }
        
        if ([[array objectAtIndex:indexPath.row] count ] ==indexPath.row+1) {
            UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 50-0.35, ScreenWidth, 0.35)];
            lineLabel.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel];
            }}
    if (indexPath.section == 1) {
        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(ScreenWidth-30,17.5, 8, 13);
        [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
        [cell.contentView addSubview:jiantou];
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[[array1 objectAtIndex:indexPath.row] objectAtIndex:1]]];
        cell.textLabel.text = [[array1 objectAtIndex:indexPath.row] objectAtIndex:0];
        
        if (indexPath.row==0) {
            UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.3)];
            lineLabel.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel];
            
        }
        if ([[array1 objectAtIndex:indexPath.row] count ]!=indexPath.row+1) {
            UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(50, 50-0.35, ScreenWidth-65, 0.3)];
            lineLabel.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel];
        }}
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell =(UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    if ([cell.textLabel.text isEqualToString:@"我的业绩"]) {
        MyresultsViewController * Login=[[MyresultsViewController alloc]init];
        [self.navigationController pushViewController:Login animated:YES];}
    if ([cell.textLabel.text isEqualToString:@"我的收藏"]) {
        DiscoverHouseholdMyCollectViewController * Login=[[DiscoverHouseholdMyCollectViewController alloc]init];
        [self.navigationController pushViewController:Login animated:YES];
        }
    if ([cell.textLabel.text isEqualToString:@"关于我们"]) {
        AboutUsViewController * Login=[[AboutUsViewController alloc]init];
        [self.navigationController pushViewController:Login animated:YES];
        }
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
       return 1;
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}
- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

@end
